<extend name="../Public/base" />
<block name="title">角色授权</block>
<block name='body'>
    <form class="form form-horizontal form-validate" id="form-ajax-Form" action="{:U('auth')}" method="post">
        <input type="hidden" name="role_id" id="role_id" value="{$detail.role_id}" />
        <p><span class="c-red">当前用户组：</span></span>{$detail.role_name}</p>
        <table class="table table-border table-bordered table-bg" id="auth_table">
            <tbody>
            <foreach name="auths" item="var">
                <if condition="($var.parent_id eq 0) and ($var.status eq 1) ">
                    <tr class="active">
                        <td>
                            <div class="check-box">
                                <input type="checkbox" class="checkAllChild" rel="{$var.menu_id}" name="auth_ids[]" value="{$var.menu_id}" <in name="var.menu_id" value="$authIds">checked="checked"</in> />
                                <label><b>{$var.menu_name}</b></label>
                            </div>

                        </td>
                    </tr>
                    <foreach name="auths" item="var2">
                        <eq name="var2.parent_id" value="$var.menu_id">
                            <if condition="$var2.status eq 1">
                                <tr>
                                    <td>
                                        <div class="check-box">
                                            <input type="checkbox" class="checkAllChild child_{$var.menu_id}" rel="{$var2.menu_id}" <in name="var2.menu_id" value="$authIds">checked="checked"</in> name="auth_ids[]" value="{$var2.menu_id}" />
                                            <label><b>{$var.menu_name}</b> > {$var2.menu_name}</label>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <foreach name="auths" item="var3">
                                            <eq name="var3.parent_id" value="$var2.menu_id">
                                                <div class="check-box">
                                                    <input type="checkbox" name="auth_ids[]" class="child_{$var.menu_id} child_{$var2.menu_id}" <in name="var3.menu_id" value="$authIds">checked="checked"</in> value="{$var3.menu_id}" />
                                                    <label>{$var3.menu_name}</label>
                                                </div>
                                            </eq>
                                        </foreach>
                                    </td>
                                </tr>
                            </if>
                        </eq>
                    </foreach>
                </if>
            </foreach>
            </tbody>
        </table>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;确认授权&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</block>
<block name="js">
<script>
    $(function(){
        $(".checkAllChild").each(function(i){
                var rel = $(this).attr("rel");
                var total_count = $(".child_"+rel).length;
                if(total_count == 0){
                     return true;
                }
                var check_count = $(".child_"+rel+"[checked='checked']").length;
                if(total_count == check_count){
                    $(this).iCheck('check');
                }
        });

        //全选/反选
        $(".checkAllChild").on("ifClicked", function (e) {
            var child = $(this).attr('rel');
            if(!$(this).is(":checked")){
                $(".child_" + child).iCheck('check');
            }else{
                $(".child_" + child).iCheck('uncheck');
            }

        });

        //点击最底层触发其父级菜单选择和反选
        $("input[type='checkbox']").on('ifClicked', function(event){
            var className = $(this).attr('class');
            var classes = className.split(' ');
            if(!$(this).is(":checked")){
                $.each(classes,function(i, n){
                    var line = n.split('_');
                    //$("input[rel='" + line[1] + "']").prop("checked", true);
                    $("input[rel='" + line[1] + "']").iCheck('check');
                });
            } else {
                $.each(classes,function(i, n){
                    if($("."+ n +":checked:not(.checkAllChild)").length - 1 <= 0){
                       // alert($("."+ n +":checked:not(.checkAllChild)").length);
                        //child_8
                        var line = n.split('_');
                        $("input[rel='" + line[1] + "']").iCheck('uncheck');
                    }
                });
            }
        });
    });
</script>
</block>
